using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Collections.Generic;

namespace Thinxoft.HttpLib
{
	/// <summary>
	/// Summary description for Xedus.
	/// </summary>
	public class Xedus : MarshalByRefObject
	{
		Settings xs;
		Dictionary<string,HttpApplication> servers;
		
		public Xedus()
		{
            servers = new Dictionary<string, HttpApplication>();
            Initialize();
		}

		void Initialize()
		{
			if(File.Exists(HttpApplication.GLOBAL_SETTINGS_FILE))
			{
				FileStream fs = new FileStream(HttpApplication.GLOBAL_SETTINGS_FILE,FileMode.Open);
				BinaryFormatter bf= new BinaryFormatter();
				xs = (Settings)bf.Deserialize(fs);
				fs.Close();
			}
			else
			{
				xs = new Settings();
				SaveSettings();
			}
		}

		public void SaveSettings()
		{
			FileStream fs = new FileStream(HttpApplication.GLOBAL_SETTINGS_FILE,FileMode.Create);
			BinaryFormatter bf= new BinaryFormatter();
			bf.Serialize(fs,xs);
			fs.Close();
		}

		public HttpApplication GetServer(string name)
		{
			return (HttpApplication)servers[name];
		}

		public Settings XedusSettings
		{
			get
			{
				return xs;
			}
		}

		public int ServerCount
		{
			get{return this.servers.Count;}
		}

		
	}
}
